package com.wbs.shop.nuby.view.base;


import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.view.listener.OnClickAdapterListenter;

import java.util.List;

/**
 * Created by Administrator on 2017/1/16 0016.
 */

public abstract class BaseRecyclerAdapter<VH extends RecyclerView.ViewHolder, B> extends RecyclerView.Adapter<VH> {

    protected List<B> datas;
    protected Context mContext;
    protected LayoutInflater inflater;
    protected View.OnClickListener mOnClickListener;
    protected OnClickAdapterListenter mOnClickAdapterListenter;

    public BaseRecyclerAdapter(Context mContext) {
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<B> datas) {
        this.datas = datas;
    }

    public List<B> getDatas() {
        return datas;
    }

    public B getItem(int position) {
        return datas.get(position);
    }

    public void addAll(List<B> addDatas) {
        if (datas != null && addDatas != null && !addDatas.isEmpty()) {
            int itemCount = getItemCount();
            datas.addAll(addDatas);
            notifyItemRangeInserted(itemCount, getItemCount());
        }
    }

    public void clear() {
        if (datas != null && !datas.isEmpty()){
            datas.clear();
            notifyDataSetChanged();
        }
    }

    public boolean isContainPosition(View v) {
        try {
            int i = (int) v.getTag();
            return i >= 0 && i < getItemCount();
        } catch (Exception e) {
            Logger.e(e);
            return false;
        }
    }

    public int getPositionByView(View v) {
        return (int) v.getTag();
    }

    public void onClickAdapter(View v) {
        if (isContainPosition(v)) {
            if (mOnClickAdapterListenter != null)
                mOnClickAdapterListenter.onClick(v, getPositionByView(v));
        }
    }

    /**
     * 获取布局id
     *
     * @return
     */
    public abstract int getItemLoayoutId();

    public int getItemLoayoutId(int viewType) {
        return -1;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        int itemLoayoutId = getItemLoayoutId();
        if (itemLoayoutId <= 0)
            itemLoayoutId = getItemLoayoutId(viewType);
        return onCreateViewHolder(inflater.inflate(itemLoayoutId, parent, false), viewType);
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        onBindViewHolder(holder, getItem(position), position);
    }

    public abstract VH onCreateViewHolder(View rootView, int viewType);


    public abstract void onBindViewHolder(VH holder, B bean, int position);

    @Override
    public int getItemCount() {
        return datas != null ? datas.size() : 0;
    }

    public void setOnClickListener(View.OnClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }

    public void setOnClickAdapterListenter(OnClickAdapterListenter mOnClickAdapterListenter) {
        this.mOnClickAdapterListenter = mOnClickAdapterListenter;
    }
}
